package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.HxProject;
import com.ruoyi.system.domain.SysRole;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.IHxProjectService;

@Controller
@RequestMapping("/system/hxProject")
public class HxProjectController extends BaseController
{
    private String prefix = "system/hxProject";
	
	@Autowired
	private IHxProjectService hxProjectService;

	
	@RequiresPermissions("system:hxProject:view")
	@GetMapping()
	public String hxProject()
	{
	    return prefix + "/hxProject";
	}
	
	
	/**
	 * 日历报表控件
	 * @description: TODO
	 * @date: 2019年8月2日 下午4:16:29
	 */
	
	@GetMapping("/dateReport")
	public String dateReport()
	{
	    return prefix + "/dateReport";
	}
	/**
	 * 项目选择列表页
	 */
	@GetMapping("/chooseProject")
	public String chooseProject()
	{
	    return prefix + "/chooseProject";
	}
	
	 @GetMapping("/userListChoose")
	    public String parent(ModelMap mmap,String participators,String participatorsName)
	    {
		 mmap.put("participators", participators);  
		 mmap.put("participatorsName", participatorsName); 
		 return "system/user/userChoose";
	    }
	
	/**
	 * 月报
	 */
	//@RequiresPermissions("system:hxMonthReport:view")
	@GetMapping("/monthReport")
	public String hxMonthReport()
	{
	    return  "system/hxMonthReport/hxMonthReport";
	}
	
   @GetMapping("/testDateReport")
    public String testDateReport(ModelMap mmap)
    {
	  
        return  "demo/testDateReport";
    }
	
	/**
	 * 查询项目列表
	 */
	@RequiresPermissions("system:hxProject:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(HxProject hxProject)
	{
		SysUser user = ShiroUtils.getSysUser();
		startPage();
		List<SysRole> roles = user.getRoles();
		List<String> roleIds = new ArrayList<String>();
		for(int i=0;i<roles.size();i++) {
			roleIds.add(roles.get(i).getRoleId().toString());
		}
		if(!(roleIds.contains("1") ||roleIds.contains("3") ||roleIds.contains("4")  ||roleIds.contains("6") )) {
			hxProject.setParticipators(user.getUserId().toString());
		}
		/*if(!(user.getUserId() == 22 || user.getUserId() == 25)) { //项目管理员操作所有项目
			hxProject.setParticipators(user.getUserId().toString());
		}*/ 
        List<HxProject> list = hxProjectService.selectHxProjectList(hxProject);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出项目列表
	 */
	@RequiresPermissions("system:hxProject:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HxProject hxProject)
    {
    	List<HxProject> list = hxProjectService.selectHxProjectList(hxProject);
        ExcelUtil<HxProject> util = new ExcelUtil<HxProject>(HxProject.class);
        return util.exportExcel(list, "hxProject");
    }
	
	/**
	 * 新增项目
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存项目
	 */
	@RequiresPermissions("system:hxProject:add")
	@Log(title = "项目", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(HxProject hxProject)
	{		
		return toAjax(hxProjectService.insertHxProject(hxProject));
	}

	/**
	 * 修改项目
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		HxProject hxProject = hxProjectService.selectHxProjectById(id);
		mmap.put("hxProject", hxProject);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存项目
	 */
	@RequiresPermissions("system:hxProject:edit")
	@Log(title = "项目", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(HxProject hxProject)
	{		
		hxProject.setStartDate(DateUtils.dateTime("yyyy-MM-dd", hxProject.getStartDateStr()));
		hxProject.setEndDate(DateUtils.dateTime("yyyy-MM-dd", hxProject.getEndDateStr()));
		return toAjax(hxProjectService.updateHxProject(hxProject));
	}
	
	/**
	 * 删除项目
	 */
	@RequiresPermissions("system:hxProject:remove")
	@Log(title = "项目", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(hxProjectService.deleteHxProjectByIds(ids));
	}
	
}
